package com.itcam.tingshu.order.handler;

import com.itcam.tingshu.common.constant.KafkaConstant;
import com.itcam.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : Cammy.Wu
 * Description : 监听消息
 */

@Slf4j
@Component
public class RedisDelayHandle {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;

    // 开启一个线程，使用定时任务
    @PostConstruct
    public void init() {
        new Thread(() -> {
            while (true) {
                // 从队列中获取数据
                RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(KafkaConstant.QUEUE_ORDER_CANCEL);
                try {
                    String orderNo = (String) blockingDeque.take();
                    log.info("订单取消定时任务，订单号：{}", orderNo);
                    orderInfoService.orderCancel(Long.parseLong(orderNo));
                } catch (InterruptedException e) {
                    e.getMessage();
                }
            }
        }).start();
    }
}
